/**
 * Created by zhuo on 2017/8/1.
 */
var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({ dest: 'uploads/' })
// import mockCompanyData from './mockData/companysMock.json';
var projects = require('./mockData/mockProject.json');
var result = require("./mockData/operationResult.json");

var createAndUpdateData = {
    "message": "测试内容82y7",
    "project": {
        "brandImgUrl": "测试内容5555",
        "contact": "测试内容efep",
        "createTime": 3993939,
        "id": "项目",
        "maintainer": "测试内容842v",
        "name": "项目",
        "remark": "测试内容r668"
    },
    "result": true
}

/* GET users listing. */
router.get('/getProjectList', function(req, res, next) {
    console.info("get projects now...");
    projects.draw = req.query.draw;
    res.json(projects);
});

router.post('/create',upload.single('img'), function(req, res, next) {
    console.info("create project now... file path is:"+req.file.path
        +" others are:"+JSON.stringify(req.body));
    res.json(createAndUpdateData);
});
router.post('/delete', function(req, res, next) {
    console.info("delete project now...:"+JSON.stringify(req.body));
    res.json(result);
});
router.post('/update',upload.single('img'), function(req, res, next) {
    console.info("update project now... body:"+JSON.stringify(req.body)
    +" file path is: "+req.file.path);

    if(req.file){//if exist
        deleteImgById(req.body.id);
    }

    // res.json(result);
    res.json(createAndUpdateData);
});

router.post('/upload', upload.single('avatar'), function (req, res, next) {
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
})

//region methods
function deleteImgById(id) {
    if(!id)return;
    console.log("delete it success!");
}
//endregion

module.exports = router;/**
 * Created by zhuo on 2017/8/1.
 */
